from dxfwrite import DXFEngine as dxf
from dxfwrite.dimlines import dimstyles, LinearDimension


class Draw():
    def drawDxf(self, fileName, contents):
        drawing = dxf.drawing(fileName)
        for content in contents:
            if content['type'] == 'point':
                drawing.add(self.drawPoint(content))
            if content['type'] == 'line':
                drawing.add(self.drawLine(content))
            if content['type'] == 'polyline':
                drawing.add(self.drawPolyline(content))
                self.drawLinearDimension(content, drawing)
            if content['type'] == 'text':
                drawing.add(self.drawText(content))
            if content['type'] == 'arc':
                drawing.add(self.drawArc(content))
            if content['type'] == 'circle':
                drawing.add(self.drawCircle(content))
        drawing.save()
        dxfFile = {
            'success': True,
            'fileName': fileName,
        }
        return dxfFile

    # drawPoint
    # content格式
    # {
    #     "type": "point",
    #     "point": [5,5],
    #     "color": 100
    # }
    def drawPoint(self, content):
        point1 = content['point']
        color = content['color']
        point = dxf.point((point1[0], point1[1]))
        point['layer'] = 'Points'
        point['color'] = color
        return point

    # drawLine
    # content格式
    # {
    #     "type": "line",
    #     "start": [0,1],
    #     "end": [0,0],
    #     "color": 7
    # }

    def drawLine(self, content):
        start = content['start']
        end = content['end']
        color = content['color']
        line = dxf.line((start[0], start[1]), (end[0], end[1]))
        line['layer'] = 'Line'
        line['color'] = color
        return line

    # drawPolyline
    # content格式
    # {
    #     "type": "polyline",
    #     "point": [[0,20],[3,20],[6,23],[9,23]],
    #     "color": 100
    # }
    def drawPolyline(self, content):
        point1 = content['point']
        color = content['color']
        polyline = dxf.polyline(linetype='DOT')
        polyline.add_vertices([(point1[0][0], point1[0][1]), (point1[1][0], point1[1][1]),
                               (point1[2][0], point1[2][1]), (point1[3][0], point1[3][1])])
        polyline['color'] = color
        polyline['layer'] = 'Polyline'
        return polyline
    # drawArc
    # content格式
    # {
    #     "type": "arc",
    #     "radius":3.0,
    #     "center": [2,3],
    #     "tangle": [30,120],
    #     "color": 7
    # }

    def drawArc(self, content):
        radius = content['radius']
        center = content['center']
        tangle = content['tangle']
        color = content['color']
        arc = dxf.arc(radius, (center[0], center[1]), tangle[0], tangle[1])
        arc['layer'] = 'Arc'
        arc['color'] = color
        return arc

    # drawCircle
    # content格式
    # {
    #     "type": "circle",
    #     "radius":0.5,
    #     "center": [6,6],
    #     "color": 7
    # }
    def drawCircle(self, content):
        radius = content['radius']
        center = content['center']
        color = content['color']
        circle = dxf.circle(radius, (center[0], center[1]))
        circle['layer'] = 'Circle'
        circle['color'] = color
        return circle

    # drawText
    # content格式
    # {
    #     "type": "text",
    #     "content":"abcdefg",
    #     "point": [2.0,1.0],
    #     "height": 0.7,
    #     "rotation": 45,
    #     "color": 7
    # }
    def drawText(self, content):
        point = content['point']
        height = content['height']
        color = content['color']
        rotation = content['rotation']
        content = content['content']
        text = dxf.text(content, (point[0], point[1]),
                        height=height, rotation=rotation)
        text['layer'] = 'TEXT'
        text['color'] = color
        # text = dxf.text('Text', (1.0, 1.0), height=0.7, rotation=45)
        # text['layer'] = 'TEXT'
        # text['color'] = 7
        return text

    # drawLinearDimension
    def drawLinearDimension(self, content, drawing):
        dimstyles.setup(drawing)
        point1 = content['point']
        points = [(point1[0][0], point1[0][1]), (point1[1][0], point1[1][1]),
                  (point1[2][0], point1[2][1]), (point1[3][0], point1[3][1])]
        dimstyles.new("dots", tick="DIMTICK_DOT",
                      scale=1., roundval=1, textabove=.1)
        dimstyles.new("dots", tick="DIMTICK_DOT",
                      scale=1., roundval=2, textabove=.5)
        dimstyles.new("arrow", tick="DIMTICK_ARROW",
                      tick2x=True, dimlineext=0.)
        dimstyles.new('dots2', tick="DIMTICK_DOT", tickfactor=.5)
        drawing.add(LinearDimension(
            (3, 3), points, dimstyle='dots', angle=15.))
        drawing.add(LinearDimension((0, 3), points, angle=90.))
        drawing.add(LinearDimension(
            (-2, 14), points, dimstyle='arrow', angle=-10))
        dimline = LinearDimension((-2, 3), points, dimstyle='dots', angle=90.)
        dimline.set_text(1, 'dimension')
        drawing.add_anonymous_block(dimline, layer='DIMENSIONS')
        return drawing
